package com.jack006.user.controller;

import com.jack006.api.BaseController;
import com.jack006.api.controller.user.AppUserMngControllerApi;
import com.jack006.enums.UserStatus;
import com.jack006.grace.result.GraceJSONResult;
import com.jack006.grace.result.ResponseStatusEnum;
import com.jack006.pojo.AppUser;
import com.jack006.user.service.AppUserMngService;
import com.jack006.user.service.UserService;
import com.jack006.utils.PagedGridResult;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;

/**
 * @Classname AppUserMngController
 * @Description 用户管理
 * @Date 2021-06-10 08:15
 * @Created by jack
 */
@RestController
public class AppUserMngController extends BaseController implements AppUserMngControllerApi {

    final static Logger logger = LoggerFactory.getLogger(AppUserMngController.class);

    @Autowired
    private AppUserMngService appUserMngService;
    @Autowired
    private UserService userService;

    @Override
    public GraceJSONResult queryAll(String nickname, Integer status, Date startDate, Date endDate,
                                    Integer page, Integer pageSize) {

        if (page == null) {
            page = COMMON_START_PAGE;
        }
        if (pageSize == null) {
            pageSize = COMMON_PAGE_SIZE;
        }

        PagedGridResult result = appUserMngService.queryAllUserList(nickname, status, startDate, endDate, page, pageSize);

        return GraceJSONResult.ok(result);
    }

    @Override
    public GraceJSONResult userDetail(String userId) {

        AppUser appUser = userService.getUserById(userId);
        return GraceJSONResult.ok(appUser);
    }

    @Override
    public GraceJSONResult freezeUserOrNot(String userId, Integer doStatus) {

        if (!UserStatus.isUserStatusValid(doStatus)) {
            return GraceJSONResult.errorCustom(ResponseStatusEnum.USER_STATUS_ERROR);
        }

        appUserMngService.freezeUserOrNot(userId,doStatus);

        // 刷新用户状态
        // 1. 删除用户会话，从而保障用户需要重新登录以后来刷新用户的会话信息
        // 2. 查询最新用户的信息，重新放入 redis中，做一次更新
        redisOperator.del(REDIS_USER_INFO+":"+userId);

        return GraceJSONResult.ok();
    }
}
